package com.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.client.pojo.Orders;
import com.client.pojo.Products;
import com.client.pojo.Users;
import com.client.service.impl.OrdersServiceImpl;
import com.client.service.impl.ProductsServiceImpl;
import com.client.service.impl.UsersServiceImpl;
import com.client.utils.RedisUtils;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品订单记录表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
public class OrdersController {
    /**
     * 添加订单
     *
     * @return
     */
    @Autowired
    OrdersServiceImpl ordersService;
    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    ProductsServiceImpl productsService;
    @Autowired
    RedisUtils redisUtils;

    @RequestMapping("/addOrder")
    ResponseMsg addOrder(@RequestBody Orders order) {
        System.out.println("添加了订单"+ order.toString());
        if (null != order) {
            Users user = usersService.getById(order.getUserId());
            Products product = productsService.getById(order.getProductId());
            if (user.getPoints() >= product.getActualPrice().doubleValue()) {
                Orders orders = order;
                //减积分,但不确定积分类型转换是否正确
                user.setPoints(user.getPoints() - product.getActualPrice().intValue());
                orders.setStatus("待发货");
                return ResponseMsg.getInstance(200, ordersService.save(orders));
            } else {
                System.out.println("兑换失败,积分不足!");
                return ResponseMsg.error();
            }
        } else {
            return ResponseMsg.error();
        }
    }

    @RequestMapping("getOrderById")
    ResponseMsg getGoodById(Integer orderId) {
        System.out.println("根据id查询了订单");
        String Rid = orderId + "goods";
        if (redisUtils.get(Rid) == null) {
            System.out.println("从数据库拿的数据");
            Orders orders = ordersService.getById(orderId);
            redisUtils.set(Rid, orders);
            return ResponseMsg.getInstance(200, orders);
        }
        System.out.println("从Redis拿的数据");
        Orders orders = (Orders) redisUtils.get(Rid);
        return ResponseMsg.getInstance(200, orders);
    }

    @RequestMapping("orderPage")
    ResponseMsg productsPage(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer limit) {
        System.out.println("order分页查询调用了");
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("order_id");
        IPage<Orders> ordersIPage = ordersService.page(new Page<>(page, limit), wrapper);
        return ResponseMsg.getInstance(200, ordersIPage);
    }

    @RequestMapping("getOrderByUserId")
    ResponseMsg getOrderByUserId(Integer userId) {
        System.out.println("根据用户id查询订单");
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        IPage<Orders> ordersIPage = ordersService.page(new Page<>(1, 5), wrapper);
        return ResponseMsg.getInstance(200, ordersIPage);
    }

    @RequestMapping("getProductByOrderId")
    ResponseMsg getProductByOrderId(Integer orderId) {
        System.out.println("根据订单id查询商品");
        Orders orders = ordersService.getById(orderId);
        Products products = productsService.getById(orders.getProductId());
        return ResponseMsg.getInstance(200, products);
    }

    /**
     * 根据用户ID去查询用户订单中的商品信息
     * @param userId
     * @return
     */
    @GetMapping("getByUserId/{userId}")
    public ResponseMsg getOrderByProductId(@PathVariable Integer userId){
        List<Orders> orders = ordersService.getProductByUserId(userId);
        return ResponseMsg.getInstance(200,"11111",orders);
    }
}

